package day05;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/14
 */
public class Sequence {
    public static void main(String[] args) {
        int n = 20;
        double sum = Sum(n);
        System.out.println("数列前20项和为 "+sum);
    }

    public static double Sum(int n) {
        double sum = 0;
        double fz = 2;   // 分子初始值
        double fm = 1; // 分母初始值

        for (int i = 1; i <= n; i++) {
            sum += fz / fm;

            // 计算下一项的分子和分母
            double nextFz = fz + fm;
            double nextFm = fz;

            fz = nextFz;
            fm = nextFm;
        }

        return sum;
    }
}
